
  const $ = require('axios');
  const {mock} = require('mockjs');
  const Common = require('../common/index');
  class Service extends Common{
    constructor() {
      super('Demo');
    };
  
    async add(ctx) {
      let data = ctx.params;
      try {
        let cTime = Date.now();
        let uTime = Date.now();
        let res = await $.post(ctx.baseURL + '/Demo', {
          id: mock('@id'),
          cTime,
          uTime,
          ...data,
        }) 
        ctx.body = {
          code: 666,
          msg: 'success',
          data: res.data
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error
        }
      }
    }
  
    async list(ctx) {    
      let {pageSize,currPage} = ctx.params; 
      try {
        let res = await $.get(ctx.baseURL+ '/Demo');
        let list = res.data;
        let total = list.length;
        // 分页
        list = this.pageing(list, currPage, pageSize);

        ctx.body = {
          total,
          pageSize,
          currPage,
          code: 666,
          msg: 'success',
          data: list
        };
      } catch (error) {
        // console.log(error);
        ctx.body = {
          code: 500,
          msg: error.message
        }
      }
    }
  
    async all(ctx) {
      try {
        let res = await $.get(ctx.baseURL + '/Demo');
        ctx.body = {
          code: 666,
          msg: 'success',
          data: res.data
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message
        }
      }
    }
  
    async edit(ctx) {
      try {
        ctx.params.uTime = Date.now();
        let res = await $.patch(ctx.baseURL + '/Demo/' + ctx.params.id, ctx.params);
        ctx.body = {
          code: 666,
          msg: 'success',
          data: res.data
        }
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message
        }
      }
    }
  
    async detail(ctx) {
      try {
        let res = await $.get(ctx.baseURL + '/Demo/' + ctx.params.id);
        ctx.body = {
          code: 666,
          msg: 'success',
          data: res.data
        }
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message
        }
      }
    }
  
    async del(ctx) {
      try {
        let res = await $.delete(ctx.baseURL + '/Demo/' + ctx.params.id);
        ctx.body = ctx.body = {
          code: 666,
          msg: 'success',
          data: res.data
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message
        }
      }
    } 
  }
  module.exports = new Service();
